layui.use(['form'], function () {
    var form = layui.form;
    var $=layui.jquery;
        

    
    //创建一个编辑器
    //var editIndex = layedit.build('LAY_demo_editor');

    //自定义验证规则
    form.verify({
        m_name: function (value) {
            if (value.length <= 0) {
                return '菜单名称可不能为空呢';
            }
        },
        m_order: [
            /^[\d]+$/, '亲！菜单序号至少一位数字以上哦'
        ],
    });

    //监听指定开关
    let status=0;
    form.on('switch(m_status)', function (data) {
        layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
            offset: '6px'
        });
        status = this.checked?1:0;
        
    });
    
    //监听提交
    form.on('submit(demo1)', function (data) {
        var index = parent.layer.getFrameIndex(window.name);
        console.log(index);
        data.field.m_status = status;
        console.log(data.field);
        $.ajax({
            url:"./addMenu",
            type:"post",
            data:data.field,
        }).then((res) => {
            if(res.status==1){
                parent.layer.msg(res.msg);
                parent.layer.close(index);
            }else{
                layer.msg(res.msg);
            }
        })
        return false;
    });

   

});